package com.zretc.kscxmz.controller.inform;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageInfo;
import com.zretc.kscxmz.entity.infor.InformEntity;
import com.zretc.kscxmz.services.InformService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("insert")
public class InformController {

    private static final Logger logger = LoggerFactory.getLogger(InformController.class);
    @Autowired
    private InformService informService;

    /*新增通知*/
    @RequestMapping("")
    @ResponseBody
    public Map<String, Object> insert(InformEntity informEntity) {
        Map<String, Object> map = new HashMap<>();
        boolean insert = informService.insert(informEntity);
        if (insert) {
            map.put("state", true);
            map.put("msg", "添加成功");
        } else {
            map.put("state", false);
            map.put("msg", "添加失败");
        }

        System.out.println(map);
        return map;
    }


    /*通知列表$ 通知管理*/
    @GetMapping("findlist")
    @ResponseBody
    public PageInfo<InformEntity> findList(@RequestParam(value = "pageNum") int pageNum,
                                           @RequestParam(value = "pageSize") int pageSize) {

        PageInfo<InformEntity> list = informService.findList(pageNum, pageSize);
        return list;
    }

    /*根据标题模糊查询数据遍历*/
    @RequestMapping("list")
    @ResponseBody
    public PageInfo<InformEntity> findAllList(@RequestParam(value = "pageNum") int pageNum,
                                              @RequestParam(value = "pageSize") int pageSize,
                                              InformEntity BiaoTi){
        PageInfo<InformEntity> allList = informService.findAllList(pageNum, pageSize, BiaoTi);
        return allList;
    }

    /*根据id删除内容*/
    @GetMapping("delect")
    @ResponseBody
    public Map<String, Object> delect(@RequestParam(value = "typeid") String typeid) {
        Map<String, Object> map = new HashMap<>();
        int typeId = Integer.parseInt(typeid);
        boolean b = informService.delctByid(typeId);

        if (b) {
            map.put("state", true);
            map.put("msg", "删除成功");
        } else {
            map.put("state", false);
            map.put("msg", "删除失败");
        }
        return map;

    }

   /* @RequestMapping("byidlist")
    public String selectByidList(InformEntity informEntity, Model model){

        InformEntity info = informService.selectByidList(informEntity);
        model.addAttribute("byid","infp");
        return "forward:admin/index/notice/xiugai.jsp";

    }*/

    /*根据id修改内容 转发到notice/xiugai*/
    @RequestMapping("/byidlist")
    public String selectByidList(Integer id, Model model) throws IOException {
        InformEntity info = informService.selectByidList(id);

        /*将java转成json/返回前端以后是个对象不用再用json转对象了*/
        ObjectMapper objectMapper = new ObjectMapper();
        String json = objectMapper.writeValueAsString(info);
        model.addAttribute("byid", json);
        logger.debug("=======" + json);
        return "/notice/xiugai";
    }


    /*根据id修改通知内容*/
    @RequestMapping("/update")
    @ResponseBody
    public Map<String,Object> update(InformEntity informEntity){

        Map<String,Object> map = new HashMap<>();
        Boolean ok = informService.updateByid(informEntity);
        if (ok){
            map.put("stats",true);
            map.put("msg","修改成功");
        }else {
            map.put("stats",false);
            map.put("msg","修改失败");

        }

        return map;
    }

    /*根据id查看通知内容*/
    @RequestMapping("chakan")
    public String selectByid(Integer id, Model model) throws JsonProcessingException {
        InformEntity informEntity = informService.selectByid(id);
            logger.debug("==============="+id);
        /*将java转成json/返回前端以后是个对象不用再用json转对象了*/
        ObjectMapper objectMapper = new ObjectMapper();
        String json = objectMapper.writeValueAsString(informEntity);
        model.addAttribute("msg",json);
        logger.debug("==============="+informEntity);
        return "/notice/chakan";
    }

}
